<template>
  <section>
    <!--工具条：关键字查询 + 新增-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <div class=" input-box mb20">
          <input type="text" class="input" v-model="keyword" placeholder="请输入搜索词" />
          <span class="span" @click="keywordQuery"><i class="el-icon-search"/></span>
        </div>
        <el-form-item>
          <el-button round plain icon="el-icon-plus" type="primary" v-perm="'customerType/add'" @click="handleAdd">新增销售渠道</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表:优秀实践，copy domain修改列表-->
    <el-table :data="roles" highlight-current-row v-loading="listLoading"
              @selection-change="selsChange" style="width: 50%;" >
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="customerType" label="客户销售渠道" width="120" :header-align="'center'" :align="'center'">
      </el-table-column>
      <el-table-column label="操作":header-align="'center'" :align="'center'">
        <template scope="scope">
          <el-button v-perm="'customerType/update'" type="primary" icon="el-icon-edit-outline" plain size="small"  @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <!-- v-perm="'role:delete'" -->
          <el-button v-perm="'customerType/del'" type="danger" plain icon="el-icon-delete"   size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination layout="total, sizes, prev, pager, next, jumper"
                     :page-sizes="[5, 10, 20, 50]"
                     @current-change="handleCurrentChange"
                     @size-change="handleSizeChange"
                     :current-page="currentPage" :page-size="pageSize"
                     :total="totals" style="float:left; margin-left: 40px;"  >
      </el-pagination>
    </el-col>

    <!--新增和编辑对话框-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <!--ref="saveForm" 指定表单名称，以后可以通过this.$refs.roleForm-->
      <el-form :model="saveForm" label-width="110px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="客户销售渠道" prop="customerType">
          <el-input v-model="saveForm.customerType" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">保存</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
export default {
  data() {
    return {
      //分页显示所属数据
      roles: [],
      listLoading: false,
      currentPage: 1,
      pageSize: 5,
      totals: 0,

      //高级查询所需数据
      keyword:'',

      //多条数据选中
      sels: [],//列表选中列

      //添加或修改数据
      saveFormVisible: false,//添加或修改界面是否显示
      saveLoading: false,
      title: null,
      saveFormRules: {
        customerType: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ],
      },
      //编辑界面数据
      saveForm: {
        id: null,
        customerType: '',
      },

    }
  },
  methods: {
    //获取角色列表
    getRoles() {
      let paras = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.keyword
      };
      this.listLoading = true;
      this.$http.post("/customerType", paras).then(res => {
        this.totals = res.data.total;
        this.roles = res.data.records;
        this.listLoading = false;
      }).catch(res => {
        this.$message.error("获取分页数据失败!!!");
      })
    },

    //关键字查询
    keywordQuery() {
      this.currentPage = 1;
      this.getRoles();
    },

    //分页查询 - 点击某一页查询
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getRoles();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1; // 重置当前页码为1
      this.getRoles();
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/customerType/" + row.id).then(res => {
          this.listLoading = false;
          if (res.data.success) {
            //计算总页数
            let totalPage = this.totals % this.pageSize == 0 ? this.totals / this.pageSize
                : Math.ceil(this.totals / this.pageSize);
            if (this.currentPage > 1 && this.currentPage == totalPage && (this.totals - 1) % this.pageSize == 0) {
              this.currentPage = this.currentPage - 1;
            }
            this.getRoles();
          } else {
            this.$message.warning("该客户销售渠道已经被使用，禁止删除！");
          }
        }).catch(res => {
          this.$message.warning("系统错误,请稍后重试！！！");
        })
      }).catch(() => {
        //确认框点击取消之后走这里
      });
    },

    //点击修改按钮弹出模态框
    handleEdit: function (index, row) {
      //修改对话框标题
      this.title = "编辑";
      //回显
      this.saveForm = Object.assign({}, row);
      this.saveFormVisible = true;
    },

    //点击添加按钮弹出对话框
    handleAdd: function () {
      //修改对话框标题
      this.title = "添加";

      //置空数据，防止先点修改，后点添加有值
      this.saveForm = {
        id: null,
        customerType: '',
        /* sn: '',
         menus: null,
         permissions: null*/
      };
      //显示对话框
      this.saveFormVisible = true;
    },

    //点击对话框提交数据
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认保存吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            let para = Object.assign({}, this.saveForm);
            //处理分配菜单参数格式 menus:[[22,23],[22,24],[22,25]]---->menus:[23,24,25]
            let menusArr = para.menus;
            if (menusArr) {
              let menusTmp = [];
              for (var i = 0; i < menusArr.length; i++) {
                let menuTmp = menusArr[i]; //[22,23]
                menusTmp.push(menuTmp[menuTmp.length - 1]);
              }
              para.menus = menusTmp;
            }
            this.$http.put("/customerType", para).then((res) => {
              this.saveFormVisible = false;
              this.saveLoading = false;
              if (res.data.success) {
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                this.$refs['saveForm'].resetFields();//重置表单
                this.getRoles(); //重新加载
              } else {
                this.$message.warning("操作失败,请稍后重试");
              }
            }).catch(res => {
              this.$message.warning("系统错误,请稍后重试!!!");
            })
          });
        }
      });
    }
    ,

    selsChange: function (sels) {
      this.sels = sels;
    }
    ,

    //批量删除
    batchRemove: function () {
      //遍历选择记录把id放到一个数组中
      var ids = this.sels.map(item => item.id); //[1,2,3]
      this.$confirm('确认删除选中记录吗？', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/customerType", ids).then((res) => {
          this.listLoading = false;
          if (res.data.success) {
            this.$message.success("操作成功");
            this.currentPage = 1;
            this.getRoles();
          } else {
            this.$message.warning("批量删除失败,请稍后重试");
          }
        }).catch(res => {
          this.$message.warning("系统错误,请稍后重试!!!");
        })
      }).catch(() => {
      });
    },



  },
  mounted() {
    this.getRoles();

  }
}

</script>

<style scoped>

</style>